// index.js
// 获取应用实例
const app = getApp()

Page({
  data: {
    treeData0: [{
            id: "1-1",
            parents: [
                {
                    nodeName: "张太祖",
                    id: "1-1-1",
                    sex:1
                },
                {
                    nodeName: "张翠花",
                    id: "1-1-2",
                    sex:0
                }
            ],
            children: [
                {
                        id: "2-1",
                        parents: [
                            {
                                nodeName: "张一上",
                                id: "2-1-1",
                                sex:1
                            },
                            {
                                nodeName: "张一女",
                                id: "2-1-2",
                                sex:0
                            }
                        ],
                        children: [
                            {
                                id: "2-1",
                                parents: [
                                    {
                                        nodeName: "张二上",
                                        id: "2-1-1",
                                        sex:1
                                    },
                                    {
                                        nodeName: "张二女",
                                        id: "2-1-2",
                                        sex:0
                                    }
                                ],
                                children:[
                                    {
                                        id: "2-2",
                                        parents: [
                                            {
                                                nodeName: "张上猪",
                                                id: "4-2-1",
                                                sex:1
                                            }
                                        ],
                                        children: []
                                    },
                                    {
                                        id: "2-2",
                                        parents: [
                                            {
                                                nodeName: "张上狗",
                                                id: "4-2-1",
                                                sex:1
                                            }
                                        ],
                                        children: []
                                    },
                                    {
                                        id: "2-2",
                                        parents: [
                                            {
                                                nodeName: "张上兔",
                                                id: "4-2-1",
                                                sex:1
                                            }
                                        ],
                                        children: []
                                    },
                                    {
                                        id: "2-2",
                                        parents: [
                                            {
                                                nodeName: "张上鸡",
                                                id: "4-2-1",
                                                sex:1
                                            }
                                        ],
                                        children: []
                                    }
                                ]
                            }
                        ]
                },
                {
                        id: "2-2",
                        parents: [
                            {
                                nodeName: "张一中",
                                id: "2-2-1",
                                sex:1
                            },
                            {
                                nodeName: "张一囡",
                                id: "2-2-2",
                                sex:0
                            }
                        ],
                        children: []
                },
                {
                    id: "2-2",
                    parents: [
                        {
                            nodeName: "张一下",
                            id: "2-2-1",
                            sex:1
                        }
                    ],
                    children: [
                        {
                            id: "2-2",
                            parents: [
                                {
                                    nodeName: "张下狗",
                                    id: "3-2-1",
                                    sex:1
                                }
                            ],
                            children: []
                        },
                        {
                            id: "2-2",
                            parents: [
                                {
                                    nodeName: "张下猫",
                                    id: "3-2-2",
                                    sex:1
                                }
                            ],
                            children: [
                                {
                                    id: "2-2",
                                    parents: [
                                        {
                                            nodeName: "张猫一",
                                            id: "4-2-1",
                                            sex:1
                                        }
                                    ],
                                    children: []
                                },
                                {
                                    id: "2-2",
                                    parents: [
                                        {
                                            nodeName: "张猫二",
                                            id: "4-2-1",
                                            sex:1
                                        }
                                    ],
                                    children: []
                                },
                            ]
                        },
                    ]
                },
                {
                    id: "2-2",
                    parents: [
                        {
                            nodeName: "张一次",
                            id: "2-2-1",
                            sex:1
                        }
                    ],
                    children: []
                }
            ]
        
  }],
    treeData: [],
    showPopBox: false,
    array: [1,2,3,4,5],
    name: '张晓明',
    popboxData: {
        boxStatus: false,
        addNode: false
    },
    currentNodeData: {},
    currentParents: [],
    currentChildren: [],
    nodeWidth: 2,
    nodeHeight: 5,
    nodeMargin: 0.5,
    nodeFontSize: 12,
    nodeStyle: '',
    personItemStyle: {},
    personItemHeight: 7.7,
    personCount: [],
  },
  onLoad() {
    this.getFamilyData()
    // this.createDom()
  },
  //引入的tree组件事件处理函数
  tapItem: function (e) {
    this.setData({ 
        popboxData: { boxStatus: true }, 
        currentNodeData: e.detail.item,
        currentParents: e.detail.parents,
        currentChildren: e.detail.children
    })
  },
  //点击出现弹窗
  tapItemFather: function(e) {
    var item = e.currentTarget.dataset.item;
    this.setData({ 
        popboxData: { boxStatus: true }, 
        currentNodeData: item,
        currentParents: []
    })
  },
  //获取家族成员列表
  getFamilyData(){
    let this_ = this
    wx.request({
        url: 'https://pg-family-tree-ranpovkkyd.cn-hangzhou.fcapp.run/ft-family/member?id=1', 
        method: 'GET',
        header: {'content-type': 'application/json'},
        success (res) {
            this_.setData({ treeData: res.data.data})
            this_.countPersonSex(res.data.data)
        }
    })
  },
  //显示弹窗
  showPop() {
    this.setData({ popboxData:{ boxStatus: true }})
  },
  //隐藏弹窗
  closePop() {
    this.setData({ popboxData:{ boxStatus: false }})
  },
  //放大节点
  larger() {
    let d =  this.data
    if(d.nodeWidth >= 2) return
    d.nodeWidth += 0.2,
    d.nodeHeight += 0.5,
    d.nodeMargin += 0.1,
    d.nodeFontSize += 2,
    d.personItemHeight += 1.6
    this.setData({ 
        nodeStyle: 'width:'+ d.nodeWidth + 'em;'+'height:'+ d.nodeHeight + 'em;' + 'margin:0 '+ d.nodeMargin +'em '+ d.nodeMargin + ' em;' + 'font-size:' + d.nodeFontSize + 'px',
        personItemStyle: 'height:' +d.personItemHeight + 'em'
    })
  },
  //缩小节点
  smaller() {
    let d =  this.data
    if(d.nodeWidth <= 1.6) return
    d.nodeWidth -= 0.2,
    d.nodeHeight -= 0.5,
    d.nodeMargin -= 0.1,
    d.nodeFontSize -= 2,
    d.personItemHeight -= 1.6
    this.setData({ 
        nodeStyle: 'width:'+ d.nodeWidth + 'em;'+'height:'+ d.nodeHeight + 'em;' + 'margin:0 '+ d.nodeMargin +'em '+ d.nodeMargin + ' em;' + 'font-size:' + d.nodeFontSize + 'px',
        personItemStyle: 'height:' +d.personItemHeight + 'em'
    })
  },
  
  //统计男女数量
  countPersonSex(data) {
    let male = 0, female = 0
        data.forEach(item=>{
            item.parents.forEach(m=>{
                m.sex == 1 ? male++ : female++
            })
            this.data.personCount.push(male+'男'+female+'女')
            if(item.children.length>0) {
                this.countPersonSex(item.children)
            }
        })
        // console.log(this.data.personCount)
  },
  //添加节点
  addNode(e) {
      let d = this.data.currentNodeData
      d.ft_level += 1
      let params = 'relation_id='+d.id+
        '&sex='+e.target.dataset.num[0]+
        '&relation_type='+e.target.dataset.num[1]+
        '&family_id='+d.family_id+
        '&ft_level='+d.ft_level
      wx.navigateTo({
        url: '../add/index?' + params
      })
  },
  //编辑节点
  updateNode(e) {
    let d = this.data.currentNodeData
    let params = 'id='+d.id
    wx.navigateTo({
      url: '../edit/index?' + params
    })
  },
  //查看节点
  queryNode(e) {
    let d = this.data.currentNodeData
    let params = 'id='+d.id
    wx.navigateTo({
      url: '../query/index?' + params
    })
  },
  //删除节点
  deleteNode() {
      let id = this.data.currentNodeData.id
      let this_ = this
      wx.request({
        url: 'https://pg-family-tree-ranpovkkyd.cn-hangzhou.fcapp.run/ft-person/delete?id=' + id, 
        method: 'GET',
        header: {'content-type': 'application/json'},
        success (res) {
            if(res.statusCode == 200) {
                this_.setData({ popboxData:{ boxStatus: false }})
                this_.getFamilyData()
            }
        }
    })
  },
  //调整排行
  adjustNode(e) {
    let d = this.data.currentNodeData
    let a = JSON.stringify(this.data.currentChildren)
    let params = 'children='+a+'&id='+d.id+'&relationId='+d.relation_id
    wx.navigateTo({
      url: '../adjust/index?' + params
    })
  },
  //查找父母
  findParents(id, level) {
    this.data.treeData
  }

})
